/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.bookkeeper.tools.cli.helpers;

import org.apache.bookkeeper.bookie.BookieShell.Command;
import org.apache.bookkeeper.tools.common.BKCommand;
import org.apache.bookkeeper.tools.framework.CliFlags;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.lang3.StringUtils;

/**
 * This is a util class that converts new cli command to old shell command.
 */
public class BookieShellCommand<CliFlagsT extends CliFlags> implements Command {

    protected final String shellCmdName;
    protected final BKCommand<CliFlagsT> bkCmd;
    protected final CompositeConfiguration conf;

    public BookieShellCommand(String shellCmdName,
                              BKCommand<CliFlagsT> bkCmd,
                              CompositeConfiguration conf) {
        this.shellCmdName = shellCmdName;
        this.bkCmd = bkCmd;
        this.conf = conf;
    }

    @Override
    public int runCmd(String[] args) throws Exception {
        return bkCmd.apply(
            shellCmdName,
            conf,
            args
        );
    }

    @Override
    public String description() {
        // format as org.apache.bookkeeper.bookie.BookieShell.MyCommand.description
        return StringUtils.isBlank(bkCmd.getUsage()) ? shellCmdName + " [options]" : bkCmd.getUsage();
    }

    @Override
    public void printUsage() {
        bkCmd.usage();
    }
}
